unit UCadastroLocalCFOP;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, UrelatorioBase, Mask, ToolEdit, ExtCtrls, DBCtrls, StdCtrls,
  CheckLst, RLPDFFilter, RLXLSFilter, RLFilters, RLHTMLFilter,
  RLDraftFilter, DB, DBTables, RLReport, Buttons, UFomCadastro, DBClient,
  Provider, Menus, ComCtrls, Grids, DBGrids;

type
  TFCadastroLocalCFOP = class(TFormCadastro)
    edCodigo: TDBEdit;
    Label14: TLabel;
    DSLocal: TDataSource;
    Label3: TLabel;
    DBLCBLocal: TDBLookupComboBox;
    DSCFOP: TDataSource;
    QCFOP: TQuery;
    Label51: TLabel;
    edCFOP: TEdit;
    DBLCFOP: TDBLookupComboBox;
    GroupBox1: TGroupBox;
    Label8: TLabel;
    Label9: TLabel;
    Label12: TLabel;
    DBLookupComboBox1: TDBLookupComboBox;
    DBLookupComboBox2: TDBLookupComboBox;
    Label11: TLabel;
    dblCredICMS: TDBLookupComboBox;
    Label10: TLabel;
    dblCredICMSSubs: TDBLookupComboBox;
    Label13: TLabel;
    dblCredIPI: TDBLookupComboBox;
    Label1: TLabel;
    dblCredPIS: TDBLookupComboBox;
    Label15: TLabel;
    dblCredCOFINS: TDBLookupComboBox;
    dblDebICMS: TDBLookupComboBox;
    dblDebICMSSub: TDBLookupComboBox;
    dblDebIPI: TDBLookupComboBox;
    dblDebPIS: TDBLookupComboBox;
    dblDebCOFINS: TDBLookupComboBox;
    DSPlanoConta: TDataSource;
    cdsCadastroLOCCFOP_CODIGO: TIntegerField;
    cdsCadastroLOCCFOP_CFOP: TIntegerField;
    cdsCadastroLOCCFOP_LOCAL: TIntegerField;
    cdsCadastroLOCCFOP_CONT_CRE_OPERACAO: TIntegerField;
    cdsCadastroLOCCFOP_CONT_DEB_OPERACAO: TIntegerField;
    cdsCadastroLOCCFOP_CONT_CRE_ICMS: TIntegerField;
    cdsCadastroLOCCFOP_CONT_DEB_ICMS: TIntegerField;
    cdsCadastroLOCCFOP_CONT_CRE_ICMS_SUBS: TIntegerField;
    cdsCadastroLOCCFOP_CONT_DEB_ICMS_SUBS: TIntegerField;
    cdsCadastroLOCCFOP_CONT_CRE_IPI: TIntegerField;
    cdsCadastroLOCCFOP_CONT_DEB_IPI: TIntegerField;
    cdsCadastroLOCCFOP_CONT_CRE_PIS: TIntegerField;
    cdsCadastroLOCCFOP_CONT_DEB_PIS: TIntegerField;
    cdsCadastroLOCCFOP_CONT_CRE_COFINS: TIntegerField;
    cdsCadastroLOCCFOP_CONT_DEB_COFINS: TIntegerField;
    cdsCadastroUSUARIO: TStringField;
    cdsCadastroDATAMODIFICACAO: TDateTimeField;
    cdsCadastroUSUARIO_CADASTRO: TStringField;
    cdsCadastroUSUARIO_AUTERACAO: TStringField;
    cdsCadastroUSUARIO_EXCLUSAO: TStringField;
    cdsCadastroDATA_CADASTRO: TDateTimeField;
    cdsCadastroDATA_EXCLUSAO: TDateTimeField;
    cdsCadastroDATA_AUTERACAO: TDateTimeField;
    cdsCadastroEXCLUIDO: TStringField;
    cdsCadastroUSUARIO_RECUPERACAO: TStringField;
    cdsCadastroDATA_RECUPERACAO: TDateTimeField;
    cdsCadastroCODIGOEMPRESA: TIntegerField;
    procedure BtnIncluirClick(Sender: TObject);
    procedure DBLCFOPEnter(Sender: TObject);
    procedure DBLCFOPKeyDown(Sender: TObject; var Key: Word;
      Shift: TShiftState);
    procedure DSCFOPDataChange(Sender: TObject; Field: TField);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure DBLookupComboBox1KeyDown(Sender: TObject; var Key: Word;
      Shift: TShiftState);
    procedure DBLCBLocalKeyDown(Sender: TObject; var Key: Word;
      Shift: TShiftState);
    procedure edCFOPExit(Sender: TObject);
    procedure BtnSalvarClick(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  FCadastroLocalCFOP: TFCadastroLocalCFOP;

implementation

uses UDmGestao, UGeral, PesquisaMovimentoEntradaSaidaEstoque,
  UPesquisaRapida;

{$R *.dfm}

procedure TFCadastroLocalCFOP.BtnIncluirClick(Sender: TObject);
begin
  inherited;
  edCFOP.Clear;
  dsCadastro.DataSet.FieldByName('LOCCFOP_CODIGO').AsInteger           := Retorna_Codigo_Generetor('GEN_LOCAL_CFOP');
  
end;

procedure TFCadastroLocalCFOP.DBLCFOPEnter(Sender: TObject);
begin
  inherited;
  If DSCFOP.DataSet.Active = false then
    DSCFOP.DataSet.Open;
end;

procedure TFCadastroLocalCFOP.DBLCFOPKeyDown(Sender: TObject;
  var Key: Word; Shift: TShiftState);
begin
  inherited;
  fun_dblookup(Key, Shift, DBLCFOP);

  if DBLCFOP.KeyValue = null then
     edCFOP.Clear;

end;

procedure TFCadastroLocalCFOP.DSCFOPDataChange(Sender: TObject;
  Field: TField);
begin
  inherited;
  if DBLCFOP.KeyValue = null then
     edCFOP.Text :=''
  else
     edCFOP.Text := DBLCFOP.ListSource.DataSet.FieldByName('CFOP_CODIGOFISCAL').AsString;
end;

procedure TFCadastroLocalCFOP.FormClose(Sender: TObject;
  var Action: TCloseAction);
begin
  inherited;
   FCadastroLocalCFOP.Release;
   FCadastroLocalCFOP := Nil;
end;

procedure TFCadastroLocalCFOP.DBLookupComboBox1KeyDown(Sender: TObject;
  var Key: Word; Shift: TShiftState);
begin
  inherited;
  if key = vk_f8 then
  begin

    Application.CreateForm(TFPesquisaRapida,FPesquisaRapida);

    FPesquisaRapida.tipo    := 'CONTA CONTABIL';
    CaPtUrA_pEsQuIsA_rApIdA := '';

    FPesquisaRapida.ShowModal;

    if CaPtUrA_pEsQuIsA_rApIdA <> '' then
      TDBLookupComboBox(sender).Field.AsVariant := CaPtUrA_pEsQuIsA_rApIdA;

    CaPtUrA_pEsQuIsA_rApIdA := '';

    Exit;

  end;

  case key of
   116 : begin //F5
          TDBLookupComboBox(Sender).ListSource.DataSet.Close;
          TDBLookupComboBox(Sender).ListSource.DataSet.Open;
         end ;
  end;

  if key = 46 then
  begin
    TDBLookupComboBox(Sender).KeyValue:=null;
    TDBLookupComboBox(Sender).DataSource.DataSet.FieldByName(TDBLookupComboBox(Sender).DataField).AsVariant:=null;
  end;

end;

procedure TFCadastroLocalCFOP.DBLCBLocalKeyDown(Sender: TObject;
  var Key: Word; Shift: TShiftState);
begin
  inherited;
  fun_dblookup(Key, Shift, DBLCBLocal);
end;

procedure TFCadastroLocalCFOP.edCFOPExit(Sender: TObject);
begin
  inherited;

 if NOT(DSCFOP.DataSet.Locate('CFOP_CODIGOFISCAL',edCFOP.Text,[])) then
 begin
   edCFOP.Clear;
   dsCadastro.DataSet.FieldByName('LOCCFOP_CFOP').AsString:='';

 end else begin

   dsCadastro.DataSet.FieldByName('LOCCFOP_CFOP').AsInteger:= DSCFOP.DataSet.FieldByName('CFOP_CODIGO').AsInteger;

 end;

end;

procedure TFCadastroLocalCFOP.BtnSalvarClick(Sender: TObject);
begin

   if dsCadastro.DataSet.FieldByName('LOCCFOP_LOCAL').IsNull then
   begin
     ShowMessage('Local obrigatorio.');
     DBLCBLocal.SetFocus;
     exit;
   end;

   if dsCadastro.DataSet.FieldByName('LOCCFOP_CFOP').IsNull then
   begin
     ShowMessage('CFOP obrigatorio.');
     DBLCFOP.SetFocus;
     exit;
   end;


  inherited;

end;

end.
